﻿using System;
using System.Windows.Forms;
using Business;
using Entity;
using DataAccess;

namespace _3LayerEntity
{
    public partial class frmSupplierts : Form
    {
        Config con = new Config();
        SuppliersBusiness sp = new SuppliersBusiness();
        Suppliers sup = new Suppliers();

        public frmSupplierts()
        {
            InitializeComponent();
        }

        public void showData()
        {
            dgvSuppliers.DataSource = sp.Suppliers();
            dgvSuppliers.Columns[0].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
        }

        public void clear()
        {
            txtCompanyName.Clear();
            txtContactName.Clear();
            txtContactTitle.Clear();
            txtAddress.Clear();
            txtCity.Clear();
            txtCountry.Clear();
            txtPostalCode.Clear();
            txtRegion.Clear();
            txtPhone.Clear();
            txtFax.Clear();
            txtHomePage.Clear();
        }

        private void frmSupplierts_Load(object sender, EventArgs e)
        {
            this.showData();
            btnInsert.Visible = false;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            clear();
            btnInsert.Visible = true;
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            sup.CompanyName1 = con.ChuanHoaChuoi(txtCompanyName.Text);
            sup.ContactName1 = con.ChuanHoaChuoi(txtContactName.Text);
            sup.ContactTitle1 = con.ChuanHoaChuoi(txtContactTitle.Text);
            sup.Address1 = con.ChuanHoaChuoi(txtAddress.Text);
            sup.City1 = con.ChuanHoaChuoi(txtCity.Text);
            sup.Region1 = con.ChuanHoaChuoi(txtRegion.Text);
            sup.PostalCode1 = con.ChuanHoaChuoi(txtPostalCode.Text);
            sup.Country1 = con.ChuanHoaChuoi(txtCountry.Text);
            sup.Phone1 = txtPhone.Text;
            sup.Fax1 = txtFax.Text;
            sup.HomePage1 = txtHomePage.Text;
            sp.Insert(sup);
            this.showData();
            this.clear();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            sup.Id1 = Convert.ToInt32(id.Text);
            sup.CompanyName1 = con.ChuanHoaChuoi(txtCompanyName.Text);
            sup.ContactName1 = con.ChuanHoaChuoi(txtContactName.Text);
            sup.ContactTitle1 = con.ChuanHoaChuoi(txtContactTitle.Text);
            sup.Address1 = con.ChuanHoaChuoi(txtAddress.Text);
            sup.City1 = con.ChuanHoaChuoi(txtCity.Text);
            sup.Region1 = con.ChuanHoaChuoi(txtRegion.Text);
            sup.PostalCode1 = con.ChuanHoaChuoi(txtPostalCode.Text);
            sup.Country1 = con.ChuanHoaChuoi(txtCountry.Text);
            sup.Phone1 = txtPhone.Text;
            sup.Fax1 = txtFax.Text;
            sup.HomePage1 = txtHomePage.Text;
            sp.Update(sup);
            MessageBox.Show("Bạn đã update " + txtCompanyName.Text.ToUpper() + " thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.showData();
            this.clear();
        }

        private void dgvSuppliers_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int Chi_So_Dong = e.RowIndex;
            //cột CompanyName ở cột thứ 0 dòng thứ?
            id.Text = dgvSuppliers[0, Chi_So_Dong].Value.ToString();
            txtCompanyName.Text = dgvSuppliers[1, Chi_So_Dong].Value.ToString();
            txtContactName.Text = dgvSuppliers[2, Chi_So_Dong].Value.ToString();
            txtContactTitle.Text = dgvSuppliers[3, Chi_So_Dong].Value.ToString();
            txtAddress.Text = dgvSuppliers[4, Chi_So_Dong].Value.ToString();
            txtCity.Text = dgvSuppliers[5, Chi_So_Dong].Value.ToString();
            txtRegion.Text = dgvSuppliers[6, Chi_So_Dong].Value.ToString();
            txtPostalCode.Text = dgvSuppliers[7, Chi_So_Dong].Value.ToString();
            txtCountry.Text = dgvSuppliers[8, Chi_So_Dong].Value.ToString();
            txtPhone.Text = dgvSuppliers[9, Chi_So_Dong].Value.ToString();
            txtFax.Text = dgvSuppliers[10, Chi_So_Dong].Value.ToString();
            txtHomePage.Text = dgvSuppliers[11, Chi_So_Dong].Value.ToString();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int Chi_So_Dong = dgvSuppliers.CurrentRow.Index;
            if (MessageBox.Show("Bạn có chắn muốn xoá " + txtCompanyName.Text + " không???", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                sp.Delete(Convert.ToInt32(dgvSuppliers[0, Chi_So_Dong].Value.ToString()));
                this.showData();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.showData();
        }

        private void xoáToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int Chi_So_Dong = dgvSuppliers.CurrentRow.Index;
            if (MessageBox.Show("Bạn có chắn muốn xoá " + txtCompanyName.Text + " không???", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                sp.Delete(Convert.ToInt32(dgvSuppliers[0, Chi_So_Dong].Value.ToString()));
                this.showData();
            }
        }
    }
}
